package com.googlecode.likesql.expression;

import com.googlecode.likesql.constant.SqlChar;

/**
 * LikeExp represents a like expression in SQL.
 * @author Johny Huang
 * @since 0.1
 * @see {@link com.googlecode.likesql.converter.LikeExpConverter LikeExpConverter}
 */
public class LikeExp {

	private String expression;
	
	private Character escapeChar;
	
	private LikeExp(String expression){
		this.expression=expression;
	}
	
	/**
	 * Returns a new instance with a like expression.
	 * 
	 * @param expression
	 * @return
	 */
	public static LikeExp like(String expression){
		return new LikeExp(expression);
	}
	
	/**
	 * Returns a new instance with a like expression starts with %.
	 * 
	 * @param expression
	 * @return
	 */
	public static LikeExp startWith(String expression){
		return new LikeExp(SqlChar.PERCENT+expression);
	}
	
	/**
	 * Returns a new instance with a like expression ends with %.
	 * 
	 * @param expression
	 * @return
	 */
	public static LikeExp endWith(String expression){
		return new LikeExp(expression+SqlChar.PERCENT);
	}
	
	/**
	 * Returns a new instance with a like expression surround with %.
	 * 
	 * @param expression
	 * @return
	 */
	public static LikeExp inAnywhere(String expression){
		return new LikeExp(SqlChar.PERCENT+expression+SqlChar.PERCENT);
	}
	
	/**
	 * Adds escape expression.
	 * 
	 * @param ch
	 * @return
	 */
	public LikeExp escape(char ch){
		escapeChar=new Character(ch);
		return this;
	}
	
	/**
	 * Gets the like expression.
	 * 
	 * @return
	 */
	public String getExpression() {
		return expression;
	}

	/**
	 * Gets escape character
	 * 
	 * @return
	 */
	public Character getEscapeChar() {
		return escapeChar;
	}
}
